// import { useRouter } from 'vue-router';
import axios from 'axios';
console.log(import.meta.env.VITE_BASE_API);
const request = axios.create({
  // baseURL: import.meta.env.VITE_BASE_API,
  // baseURL: '/',
  timeout: 1 * 1000 * 60, // 超时时间
});
// const router = useRouter();
// 路由拦截
request.interceptors.request.use(
  config => {
    // 请求拦截

    // 添加验证
    config.headers.Authorization = `Bearer ${sessionStorage.getItem('token')}`;
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截
request.interceptors.response.use(
  response => {
    const { authorization } = response.headers;
    authorization && sessionStorage.setItem('token', authorization);
    return response.data;
  },
  error => {
    const { status } = error.response;
    // 没权限跳转登陆
    if (status === 401) {
      sessionStorage.removeItem('token');
      window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);

export default request;
